package session01;

import javax.swing.*;
import java.sql.*;

public class TestPractise {
    public static void main(String[] args) {
        String name = JOptionPane.showInputDialog(null, "请输入用户名");
        String pwd = JOptionPane.showInputDialog(null, "请输入密码");
        query(name, pwd);
    }

    /**
     * 与数据库进行用户名，密码配对
     *
     * @param name
     * @param pwd
     */
    private static void query(String name, String pwd) {
        String url = "jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=utf8";
        String user = "root";
        String password = "123456";

        try (Connection con = DriverManager.getConnection(url, user, password);
             Statement st = con.createStatement()) {
            String sql = "select f_username,f_password from t_user where f_username='" + name + "' and f_password='" + pwd + "'";
            System.out.println(sql);
            ResultSet rs = st.executeQuery(sql);
            boolean flag = false;
            while (rs.next()) {
                flag = true;
            }
            if (flag) {
                JOptionPane.showMessageDialog(null, "登陆成功");
            } else {
                JOptionPane.showMessageDialog(null, "登陆失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

/*        String sql = "select f_username,f_password from t_user where f_username= ? and f_password= ?";
        try (Connection con = DriverManager.getConnection(url, user, password);
             PreparedStatement ps = con.prepareStatement(sql)) {

            ps.setString(1, name);
            ps.setString(2, pwd);
            ResultSet rs = ps.executeQuery();

            boolean flag = false;
            while (rs.next()) {
                flag = true;
            }
            if (flag) {
                JOptionPane.showMessageDialog(null, "登陆成功");
            } else {
                JOptionPane.showMessageDialog(null, "登陆失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }*/
    }
}
